﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Configuration;
using Lswweb.Model;

namespace Lswweb.IDAL
{
    public class DALFactory
    {
        internal static readonly string AssemblyDAL = ConfigurationManager.AppSettings["DALAssembly"];

        public static ISiteSettings CreateSiteSettings()
        {   
            return DALFactory<ISiteSettings>.CreateDAL("SiteSettingsDAL");
        }

        public static IUsers CreateUsers()
        {
            return DALFactory<IUsers>.CreateDAL("UsersDAL");
        }

        public static IGuestBook CreateGuestBook()
        {
            return DALFactory<IGuestBook>.CreateDAL("GuestBookDAL");
        }

        public static INews CreateNewsDAL()
        {
            return DALFactory<INews>.CreateDAL("NewsDAL");
        }

        public static INewsSkins CreateNewsSkinDAL()
        {
            return DALFactory<INewsSkins>.CreateDAL("NewsSkinDAL");
        }

        public static ISinglePages CreateSinglePagesDAL()
        {
            return DALFactory<ISinglePages>.CreateDAL("SinglePagesDAL");
        }

        public static IProducts CreateProductDAL()
        {
            return DALFactory<IProducts>.CreateDAL("ProductsDAL");
        }

        public static ICommentary CreateCommentary()
        {
            return DALFactory<ICommentary>.CreateDAL("CommentaryDAL");
        }

        public static IOrders CreateOrdersDAL()
        {
            return DALFactory<IOrders>.CreateDAL("OrderDAL");
        }

        public static IUpFiles CreateUpFilesDAL()
        {
            return DALFactory<IUpFiles>.CreateDAL("UpFilesDAL");
        }

        public static ISubscribe CreateSubscribe()
        {
            return DALFactory<ISubscribe>.CreateDAL("SubscribeDAL");
        }

        public static ILink CreateLink()
        {
            return DALFactory<ILink>.CreateDAL("LinkDAL");
        }

        public static IProductStorage CreateProductStorageDAL()
        {
            return DALFactory<IProductStorage>.CreateDAL("ProductStorageDAL");
        }

        public static IMessages CreateMessagesDAL()
        {
            return DALFactory<IMessages>.CreateDAL("MessagesDAL");
        }

        public static ICurrencyDAL CreateCurrencyDAL()
        {
            return DALFactory<ICurrencyDAL>.CreateDAL("CurrencyDAL");
        }

        public static IProductWholesaleRuleDAL CreateProductWholesaleRuleDAL()
        {
            return DALFactory<IProductWholesaleRuleDAL>.CreateDAL("ProductWholesaleRuleDAL");
        }

        public static INewsletterDAL CreateNewsletterDAL()
        {
            return DALFactory<INewsletterDAL>.CreateDAL("NewsletterDAL");
        }

        public static ILogisticsCompanyDAL CreateLogisticsCompanyDAL()
        {
            return DALFactory<ILogisticsCompanyDAL>.CreateDAL("LogisticsCompanyDAL");
        }

        public static ILogisticsAccountingDAL CreateLogisticsAccountingDAL()
        {
            return DALFactory<ILogisticsAccountingDAL>.CreateDAL("LogisticsAccountingDAL");
        }

        public static ITreeTable<CategoryInfo> CreateTreeTableDAL(TreeTableType type)
        {
            return DALFactory<ITreeTable<CategoryInfo>>.CreateDAL("TreeTableDAL", type);
        }
    }

    internal class DALFactory<T>
    {
        #region 内部方法

        internal static T CreateDAL(string className)
        {
            string classNameStr = DALFactory.AssemblyDAL + "." + className;
            return (T)Assembly.Load(DALFactory.AssemblyDAL).CreateInstance(classNameStr);
        }
        internal static T CreateDAL(string className, params object[] args)
        {
            Assembly assembly = Assembly.Load(DALFactory.AssemblyDAL);
            string classNameStr = DALFactory.AssemblyDAL + "." + className;
            Type t = assembly.GetType(classNameStr);
            return (T)Activator.CreateInstance(t, args);
        }

        #endregion
    }
}
